#!/bin/bash
cd $(dirname $0);
vir_name=$1
exsi_Auth="root:'Pass@123'"
exsi_host=192.168.31.215
if [ $# -eq 0 ];then
   echo "There is no machien name!!!"
   exit 127;
fi
mkdir -p machines;
set +x;


vm_id=`ssh -o StrictHostKeyChecking=no root@${exsi_host} " vim-cmd vmsvc/getallvms|grep ${vir_name}|awk '{printf\$1}'"`
vm_status=`ssh -o StrictHostKeyChecking=no root@${exsi_host} " vim-cmd vmsvc/power.getstat ${vm_id}|grep Powered|awk '{printf\$2}'"`
./ovftool --powerOffSource -o --X:logFile=./machines/${vir_name}.log --noSSLVerify  vi://${exsi_Auth}@${exsi_host}/${vir_name} ./machines/${vir_name}_`date +%Y%m%d`.ova

if [ "${vm_status}" == "on" ];then

    ssh -o StrictHostKeyChecking=no root@${exsi_host} " vim-cmd vmsvc/power.on ${vm_id}
fi;